/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.message;

import hextd.message.MessageFactory.NetworkMessage;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessagePlayerAuth implements Message {

	public MessageFactory.NetworkMessage type = NetworkMessage.PlayerAuth;
	public static String RegExp = "AUTH ([^\\n]+)(\\n|$)";
	public static Pattern MessagePattern = Pattern.compile(MessagePlayerAuth.RegExp);
	//
	private String message;
	private String token;
	private int senderId = -2;

	public MessagePlayerAuth() {
	}

	private MessagePlayerAuth(String message) {
		this.message = message;
		Matcher matcher = MessagePlayerAuth.MessagePattern.matcher(message);
		if (matcher.matches()) {
			token = matcher.group(1);
		}
	}

	public static String formatMessage(String token) {
		return "AUTH " + token.replace('ː', ':');
	}

	public String getToken() {
		return this.token;
	}

	@Override
	public String getMessage() {
		return this.message;
	}

	@Override
	public Message initFromString(String message) {
		return new MessagePlayerAuth(message);
	}

	@Override
	public int getSenderId() {
		return this.senderId;
	}

	@Override
	public void setSenderId(int id) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public Pattern getPattern() {
		return MessagePlayerAuth.MessagePattern;
	}

	@Override
	public NetworkMessage getType() {
		return this.type;
	}
}
